<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    {{dataForm}}--
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="名称" prop="spec.specName">
      <el-input v-model="dataForm.spec.specName" placeholder="名称" ></el-input>
    </el-form-item>
      <el-button type="primary" plain @click="addSpec">新增规格选项</el-button>
    </el-form>
    <el-table
      :data="dataForm.options"
      style="width: 100%;">
      <el-table-column
        header-align="center"
        align="center"
        width="250"
        label="规格选项">
        <template slot-scope="scope" >
          <el-input v-model="scope.row.optionName"  placeholder="规格名称"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="250"
        label="排序">
        <template slot-scope="scope">
          <el-input v-model="scope.row.orders" placeholder="排序"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="200"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="delSpec(scope.$index)" type="danger" size="small" plain>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          spec:{
            id:undefined,
            specName: ''
          },
          options:[]
        },
        dataRule: {
          spec:{
            specName: [
              { required: true, message: '名称不能为空', trigger: 'blur' }
            ]
          }

        }
      }
    },
    methods: {
      //1. 新增规格选项
      addSpec(){
        this.dataForm.options.push({});
      },
      //2. 删除规格
      delSpec(index){
        this.dataForm.options.splice(index,1);   //参数1: 代表要删除元素在数组中的下标，参数2：代表删除的个数
      },
      init (id) {
        this.dataForm.spec.id = id || 0
        this.visible = true
        if(!id){
          this.dataForm={spec:{},options:[]}
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()    //重设表单
          if (this.dataForm.spec.id) {            //修改
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/info/${this.dataForm.spec.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                //1. 规格名称
                this.dataForm.spec.specName = data.specification.spec.specName;
                //2. 规格选项
                this.dataForm.options = data.specification.options;
                console.log("data:",data);
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/${!this.dataForm.spec.id ? 'save' : 'update'}`),
              method: `${this.dataForm.spec.id ? 'put':'post'}`,
              // data: this.$http.adornData({
              //   'id': this.dataForm.spec.id || undefined,
              //   'specName': this.dataForm.spec.specName
              // })
              data:this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
